package generator.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class EndingGenerator {

	
	private List<String> sadEndings;
	private List<String> neutralEndings;
	private List<String> persons;
	
	public EndingGenerator()  {
		sadEndings = new ArrayList<String>();
		neutralEndings = new ArrayList<String>();
		persons = new ArrayList<String>();
	}
	
	public EndingGenerator(String filename) {
		
		sadEndings = new ArrayList<String>();
		neutralEndings = new ArrayList<String>();
		persons = new ArrayList<String>();
		
		try {
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename));
			
			HSSFSheet s = wb.getSheet("Avslut");
			
			//Iterate through rows
			for(int r=s.getFirstRowNum(); r<=s.getLastRowNum(); r++) {
				HSSFRow row = s.getRow(r);
				
				HSSFCell endingCell = row.getCell(0);
				HSSFCell typeCell = row.getCell(1);		
				if(endingCell!=null && typeCell != null) {
					
					//get string values
					String ending = endingCell.getStringCellValue();
					//get ending value in upper case
					String typeString = typeCell.getStringCellValue().toUpperCase();
					
					//get type 
					EndingType type = EndingType.ALL;
					if(typeString.equals("NEUTRAL")) {
						type = EndingType.NEUTRAL;
					} else if (typeString.equals("SAD")) {
						type = EndingType.SAD;
					}
					//System.out.println(typeString);
					//add the ending
					this.addEnding(ending, type);
				}
			}
			
			//Persons
			HSSFSheet s2 = wb.getSheet("Personer");
			
			//Iterate through rows
			for(int r=s2.getFirstRowNum(); r<=s2.getLastRowNum(); r++) {
				HSSFRow row = s2.getRow(r);
				
				HSSFCell cell = row.getCell(0);
				
				if(cell != null) {
					this.addPerson(cell.getStringCellValue());
				}
			}

				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addEnding(String ending, EndingType type) {
		if(type.equals(EndingType.SAD) || type.equals(EndingType.ALL)) {
			sadEndings.add(ending);
		}
		
		if(type.equals(EndingType.NEUTRAL) || type.equals(EndingType.ALL)) {
			neutralEndings.add(ending);
		}
		//System.out.println(type.toString()+" - "+ending);
	}
	
	public void addPerson(String person) {
		persons.add(person);
	}
	
	public String getEnding(EndingType type)  {
		Random r = new Random();
		List<String> endings;
		if(type.equals(EndingType.SAD)) {
			endings = this.sadEndings;
		} else if(type.equals(EndingType.NEUTRAL)) {
			endings = this.neutralEndings;
		} else {
			endings = new ArrayList<String>(this.neutralEndings);
			endings.addAll(this.sadEndings);
		}
		
		String ending = endings.get(r.nextInt(endings.size()));
		
		if (ending.contains("%P%")) {
			ending = ending.replaceAll("%P%", persons.get(r.nextInt(persons.size())));
		}
		
		//System.out.println(type+" ending: "+ending);
		
		return ending;
	}
	
	
	public enum EndingType{
		SAD,
		NEUTRAL,
		ALL
	}
}
